package com.briup.web;

import com.briup.bean.User;
import com.briup.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author adam
 * @date 2023/2/26
 */
@Controller
public class IndexController {

    @Autowired
    IUserService userService;

    @GetMapping("/")
    public String index() {


        return "login";
    }

    @PostMapping("ajax/checkUserName")
    public void checkUserName(String name, HttpServletResponse resp) throws IOException {
        String msg = "用户名可用";
        if (StringUtils.hasLength(name)) {
            User user = userService.findUserByName(name);
            if (user != null) {
                //数据库中么有,当前用户名不可以用
                msg = "用户名不可用";
            }
        }
        resp.setContentType("text/plain;charset=utf-8");
        PrintWriter pw = resp.getWriter();
        pw.println(msg);
        pw.flush();
    }
}
